//arr {id,name} keyName中文的key 没有默认arr里面就是中文
export const pySegSort = (arr, keyName) => {
  if (!String.prototype.localeCompare) return null

  let letters = '*abcdefghjklmnopqrstwxyz'.split('')

  let zh = '阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀'.split('')

  let segs = []

  let curr

  letters.forEach(function(item, i) {
    curr = { letter: item, dataSource: [] }

    arr.forEach(function(item2) {
      let itemvalue = item2
      if (keyName) {
        itemvalue = item2[keyName]
      }
      if (
        (!zh[i - 1] || zh[i - 1].localeCompare(itemvalue) <= 0) &&
        itemvalue.localeCompare(zh[i]) == -1
      ) {
        curr.dataSource.push(item2)
      }
    })

    if (curr.dataSource.length) {
      segs.push(curr)

      curr.dataSource.sort(function(a, b) {
        let aname = a
        let bname = b
        if (keyName) {
          aname = a[keyName]
          bname = b[keyName]
        }
        return aname.localeCompare(bname)
      })
    }
  })

  return segs
}
